package com.base.cn.platform.os.controller.mobile.agent.settlement;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.service.course.agent.income.AgentIncomeInfoService;
import com.base.cn.platform.os.service.course.agent.settlement.AgentSettlementService;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 推广结算Controller
 * @author LIZHIPENG
 * @create 2018-07-02 下午 1:58
 */
@Controller
@RequestMapping("/mobile/uc/agent/settlement")
public class MobileAgentSettlementController extends MobileBaseController {

    @Autowired
    private AgentUserService agentUserService;
    @Autowired
    private AgentIncomeInfoService agentIncomeInfoService;
    @Autowired
    private AgentSettlementService agentSettlementService;

    /**
     * 初始化结算列表页面
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/initAllSettlementListView")
    public String initAllSettlementListView(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, false, false, false);
        if(ObjectUtils.isNotEmpty(agentUser)){
            return resultPage("/agent/settlement/agent-settlement-list");
        }else {
            return redirect(request, "/mobile/uc/agent/toOpenExtensionView");
        }
    }

    /**
     * ajax加载结算列表
     * @param model Model
     * @param request HttpServletRequest
     * @param currentPage 当前页码
     * @return String
     */
    @RequestMapping("/findAgentSettlementList")
    public String findAgentSettlementList(Model model, HttpServletRequest request,
                                          @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, false, false, false);
        if(ObjectUtils.isNotEmpty(agentUser)){
            Map<String, Object> params = new HashMap<>();
            params.put("agentUserId", agentUser.get("id"));
            Map<String, Object> dataMap = agentSettlementService.querySettlementListPage(currentPage, params, false);
            model.addAttribute("dataMap", dataMap);
            return resultPage("/agent/settlement/ajax-agent-settlement-list");
        }else {
            return redirect(request, "/mobile/uc/agent/toOpenExtensionView");
        }
    }

    /**
     * 查看每一期结算详情
     * @param model Model
     * @param request HttpServletRequest
     * @param settlementId 结算ID
     * @return String
     */
    @RequestMapping("/findAgentSettlementById")
    public String findAgentSettlementById(Model model, HttpServletRequest request,
                                          @RequestParam("settlementId") BigDecimal settlementId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> agentUser = agentUserService.findAgentUserByUserId(userId, false, false, false);
        if(ObjectUtils.isNotEmpty(agentUser)){
            Map<String, Object> settlement = agentSettlementService.findSettlementById(settlementId, false);
            model.addAttribute("settlement", settlement);
            return resultPage("/agent/settlement/agnet-settlement-info");
        }else {
            return redirect(request, "/mobile/uc/agent/toOpenExtensionView");
        }
    }

    /**
     * 发起结算
     * @param request HttpServletRequest
     * @return Map<String, Object>
     */
    @RequestMapping("/confirmSettlement")
    @ResponseBody
    public Map<String, Object> confirmSettlement(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("settlementFrom",1);
        return agentSettlementService.confirmSettlement(params, userId);
    }
}
